<?php

/* Author: tbloc@cantho.gov.vn */
/* Created Date : 16-07-2014 08:13:58 */

class M_Hang_hoa {

//Properties
    private $HH_ID;
    private $HH_TEN;
    private $HH_DONGIA;
    private $HH_HINHANH;
    private $HH_CHITIET;
    private $LH_ID;
    private $HH_MAUSAC;
    private $HH_LUOTXEM;

//properties set get 
    public function setHH_ID($HH_ID) {
        $this->HH_ID = $HH_ID;
    }

    public function getHH_ID() {
        return $this->HH_ID;
    }

    public function setHH_LUOTXEM($HH_LUOTXEM) {
        $this->HH_LUOTXEM = $HH_LUOTXEM;
    }

    public function getHH_LUOTXEM() {
        return $this->HH_LUOTXEM;
    }

    public function setHH_MAUSAC($HH_MAUSAC) {
        $this->HH_MAUSAC = $HH_MAUSAC;
    }

    public function getHH_MAUSAC() {
        return $this->HH_MAUSAC;
    }

    public function setHH_TEN($HH_TEN) {
        $this->HH_TEN = $HH_TEN;
    }

    public function getHH_TEN() {
        return $this->HH_TEN;
    }

    public function setHH_DONGIA($HH_DONGIA) {
        $this->HH_DONGIA = $HH_DONGIA;
    }

    public function getHH_DONGIA() {
        return $this->HH_DONGIA;
    }

    public function setHH_HINHANH($HH_HINHANH) {
        $this->HH_HINHANH = $HH_HINHANH;
    }

    public function getHH_HINHANH() {
        return $this->HH_HINHANH;
    }

    public function setHH_CHITIET($HH_CHITIET) {
        $this->HH_CHITIET = $HH_CHITIET;
    }

    public function getHH_CHITIET() {
        return $this->HH_CHITIET;
    }

    public function setLH_ID($LH_ID) {
        $this->LH_ID = $LH_ID;
    }

    public function getLH_ID() {
        return $this->LH_ID;
    }

//crud methods
    public function insert($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `hang_hoa`(                        
			`HH_ID`, 
				`HH_TEN`, 
				`HH_DONGIA`, 
				`HH_HINHANH`, 
				`HH_CHITIET`, 
				`LH_ID` 
		)
	VALUES(
    		:HH_ID, 
	    		:HH_TEN, 
	    		:HH_DONGIA, 
	    		:HH_HINHANH, 
	    		:HH_CHITIET, 
	    		:LH_ID 
	    );";
            $parameters = array(
                ":HH_ID",
                ":HH_TEN",
                ":HH_DONGIA",
                ":HH_HINHANH",
                ":HH_CHITIET",
                ":LH_ID"
            );
            $values = array(
                $this->getHH_ID(),
                $this->getHH_TEN(),
                $this->getHH_DONGIA(),
                $this->getHH_HINHANH(),
                $this->getHH_CHITIET(),
                $this->getLH_ID()
            );
            $types = array(
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_INT
            );
            $lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
            $this->setHH_ID($lastInsertID);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "update `hang_hoa`
	                set `is_active`=0
	                where `HH_ID` = :HH_ID;";
            $parameters = array(":HH_ID");
            $values = array($this->getHH_ID());
            $types = array(PDO::PARAM_INT); //Kiem tra lai kieu du lieu nhe
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function update($db = null) {
        if ($db != null) {
            $sql = "UPDATE `hang_hoa` SET 
			`HH_ID`=:HH_ID,
				`HH_TEN`=:HH_TEN,
				`HH_DONGIA`=:HH_DONGIA,
				`HH_HINHANH`=:HH_HINHANH,
				`HH_CHITIET`=:HH_CHITIET,
				`LH_ID`=:LH_ID
			WHERE `HH_ID` = :HH_ID;";
            $parameters = array(
                ":HH_ID",
                ":HH_TEN",
                ":HH_DONGIA",
                ":HH_HINHANH",
                ":HH_CHITIET",
                ":LH_ID"
            );
            $values = array(
                $this->getHH_ID(),
                $this->getHH_TEN(),
                $this->getHH_DONGIA(),
                $this->getHH_HINHANH(),
                $this->getHH_CHITIET(),
                $this->getLH_ID()
            );
            $types = array(
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_INT
            );
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }
    public function tangLuotXem($db = null) {
        if ($db != null) {
            $sql = "UPDATE hang_hoa set HH_LUOTXEM=HH_LUOTXEM+1 WHERE HH_ID=:HH_ID";
            $parameters = array(
                ":HH_ID"                
            );
            $values = array(
                $this->getHH_ID()                
            );
            $types = array(
                PDO::PARAM_INT               
            );
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }
    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                HH_ID,
                HH_TEN,
                HH_DONGIA,
                HH_HINHANH,
                HH_CHITIET,
                HH_MAUSAC,
                HH_LUOTXEM,
                LH_ID
                FROM hang_hoa WHERE HH_ID = :HH_ID;";
            $parameters = array(":HH_ID");
            $values = array($this->getHH_ID());
            $types = array(PDO::PARAM_INT); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setHH_ID($row->HH_ID);
                $this->setHH_TEN($row->HH_TEN);
                $this->setHH_DONGIA($row->HH_DONGIA);
                $this->setHH_HINHANH($row->HH_HINHANH);
                $this->setHH_CHITIET($row->HH_CHITIET);
                $this->setHH_MAUSAC($row->HH_MAUSAC);
                $this->setHH_LUOTXEM($row->HH_LUOTXEM);
                $this->setLH_ID($row->LH_ID);
            }
            return $this;
        }
    }

    public function getList($db = null) {
        if ($db != null) {
            $sql = "SELECT
                    HH_ID,
                    HH_TEN,
                    HH_DONGIA,
                    HH_HINHANH,
                    HH_CHITIET,
                    LH_ID,
                    HH_MAUSAC,
                    HH_LUOTXEM
                    FROM hang_hoa order by HH_ID desc";
            $rows = $db->executeQuery($sql);
            $objects = array();
            if ($rows != null) {
                while ($row = $rows->fetch()) {
                    $obj = new M_Hang_hoa();
                    $obj->setHH_ID($row->HH_ID);
                    $obj->setHH_TEN($row->HH_TEN);
                    $obj->setHH_DONGIA($row->HH_DONGIA);
                    $obj->setHH_HINHANH($row->HH_HINHANH);
                    $obj->setHH_CHITIET($row->HH_CHITIET);
                    $obj->setHH_MAUSAC($row->HH_MAUSAC);
                    $obj->setHH_LUOTXEM($row->HH_LUOTXEM);
                    $obj->setLH_ID($row->LH_ID);
                    $objects[] = $obj;
                }
                return $objects;
            }
        }
    }

    public function getListByLoaiHangID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                    HH_ID,
                    HH_TEN,
                    HH_DONGIA,
                    HH_HINHANH,
                    HH_CHITIET,
                    LH_ID,
                    HH_MAUSAC,
                    HH_LUOTXEM
                    FROM hang_hoa where LH_ID=:LH_ID";
            $parameters = array(":LH_ID");
            $values = array($this->getLH_ID());
            $types = array(PDO::PARAM_INT); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            $objects = array();
            if ($rows != null) {
                while ($row = $rows->fetch()) {
                    $obj = new M_Hang_hoa();
                    $obj->setHH_ID($row->HH_ID);
                    $obj->setHH_TEN($row->HH_TEN);
                    $obj->setHH_DONGIA($row->HH_DONGIA);
                    $obj->setHH_HINHANH($row->HH_HINHANH);
                    $obj->setHH_CHITIET($row->HH_CHITIET);
                    $obj->setHH_MAUSAC($row->HH_MAUSAC);
                    $obj->setHH_LUOTXEM($row->HH_LUOTXEM);
                    $obj->setLH_ID($row->LH_ID);
                    $objects[] = $obj;
                }
                return $objects;
            }
        }
    }

}
?>

